"""
波士顿房屋价格预测
"""
import pandas
import sklearn.datasets as sd
import sklearn.model_selection as ms # 模型选择模块
import sklearn.linear_model as lm
import sklearn.metrics as sm
import pandas as pd
import sklearn.pipeline as pl
import sklearn.preprocessing as sp
import sklearn.tree as st # 决策树模块
import sklearn.ensemble as se # 集成学习模块
import matplotlib.pyplot as plt

# data = pd.read_csv('boston.csv')
# 1.加载数据
data = sd.load_iris()
# print(data.keys())
# print(data.DESCR)
# print(data.data.shape)
# print(data.target.shape)

# 2.整理输入和输出
x = data.data
y = data.target


# train_list = []
# test_list = []
# idx = 1
# for i in x:
#     if idx % 10 == 0:
#         test_list.append(i)
#     else:
#         train_list.append(i)
# 3.划分训练集和测试集
# random_state：为随机种子 test_size：测试集数量
train_x,test_x,train_y,test_y = ms.train_test_split(x,y,test_size=0.1,random_state=7)
# res = ms.train_test_split(x,y,test_size=0.1)
# print(len(res))
# print(res[0].shape) # train_x
# print(res[1].shape) # test_x
# print(res[2].shape) # train_y
# print(res[3].shape) # test_y

# 线性回归
model = lm.LinearRegression()
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('线性回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('线性回归测试集r2:',sm.r2_score(test_x,pred_test_y))

# 岭回归
model = lm.Ridge(alpha=1)
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('岭回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('岭回归测试集r2:',sm.r2_score(test_x,pred_test_y))

# 多项式回归
model = pl.make_pipeline(sp.PolynomialFeatures(2),lm.LinearRegression)
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('多项式回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('多项式测试集r2:',sm.r2_score(test_x,pred_test_y))

# 决策树回归
model = st.DecisionTreeRegressor(max_depth=6)
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('决策树回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('决策树测试集r2:',sm.r2_score(test_x,pred_test_y))


# Adaboost 正向激励
model = st.DecisionTreeRegressor(max_depth=4)
model = se.AdaBoostRegressor(model,n_estimators=400,random_state=7)
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('Adaboost回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('Adaboost测试集r2:',sm.r2_score(test_x,pred_test_y))

# 特征重要性
fi = model.feature_importances_
# print(fi)
# 使用pandas进行绘图
fi = pandas.Series(fi,index=data.feature_names)
# print(fi)
fi = fi.sort_values(ascending=False)
fi.plot.bar(rot=45)
plt.show()

# GBDT
model = se.GradientBoostingRegressor(max_depth=4,n_estimators=400,min_samples_split=5)
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('GBDT回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('GBDT测试集r2:',sm.r2_score(test_x,pred_test_y))


# 随机森林
model = se.RandomForestRegressor(max_depth=4,n_estimators=400,min_samples_split=5)
model.fit(train_x,train_y)
pred_test_y = model.predict(test_x)
pred_train_y = model.predict(train_x)
print('随机森林回归训练集r2:',sm.r2_score(train_y,pred_train_y))
print('随机森林测试集r2:',sm.r2_score(test_x,pred_test_y))